<template>
  <div class='pages clientTypeMg'>
    <div class="pageLevel">
      <!-- 表格 -->
      <div class="tableMain">
        <el-table ref="multipleTable" :data="tableData" stripe style="width: 100%">
          <el-table-column type="index" label="序号" width="80" align='center'></el-table-column>
          <el-table-column prop="name" align="center" label="客户类型">
            <template slot-scope="scope">
              <span v-if='scope.row.show'>{{scope.row.name}}</span>
              <el-input v-else v-model="scope.row.name" placeholder="请输入客户类型名称20字以内" clearable size="small" maxlength='20'></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="customer_type" align="center" label="说明">
            <template slot-scope="scope">
              <span>{{scope.row.customerType == 0 ? '预置' : '自定义'}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="name" align="center" label="操作">
            <template slot-scope="scope">
              <template v-if='scope.row.show'>
                <!--                    <el-switch v-model="scope.row.status" @change='handleSwitchChange($event)'-->
                <!--                               :active-value="0" :inactive-value="1"></el-switch>-->
                <el-button size="small" type="text" @click='startOrStop(scope.row.id,0)' :disabled='scope.row.status ? false : true'>启用</el-button>
                <el-button size="small" type="text" @click='startOrStop(scope.row.id,1)' :disabled='scope.row.status ? true : false'>停用</el-button>
              </template>
              <template v-else>
                <el-button size="small" type="text" @click='save(scope.row.name)'>保存</el-button>
                <el-button size="small" type="text" @click='cancel()'>取消</el-button>
              </template>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 操作框 -->
      <div class="operationBox tableSearch">
        <el-button type="primary" @click="add()" size="mini" :disabled='dis'>新增</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { selectShopTypeList, saveBaseShopType, updateStatus } from "@/api";
export default {
  name: "client-typeMg",
  data() {
    return {
      dis: false,
      tableData: []
    };
  },
  computed: mapState(["merchantId"]),
  created() {
    this.getTableList();
  },
  mounted() {},
  methods: {
    getTableList() {
      let that = this;
      let data = {
        shopId: this.merchantId,
        name: "",
        status: ""
      };
      selectShopTypeList(data).then(res => {
        if (res.code == 200) {
          that.tableData = res.data;
          for (let i = 0; i < res.data.length; i++) {
            that.tableData[i].show = true;
          }
        } else {
          this.tips(res.msg, "error");
        }
      });
    },
    // 新增-添加表的一行
    add() {
      if (this.tableData.length > 28) {
        this.tips("客户类型数量已达上线(29)", "error");
        return;
      }
      this.dis = true;
      let newele = {
        customerType: 1,
        name: "",
        show: false
      };
      this.tableData.push(newele);
    },
    handleSwitchChange(val) {
      console.log(val);
    },
    save(val) {
      if (!val) {
        this.tips("请输入客户类型！", "error");
        return;
      }
      let data = {
        shopId: this.merchantId,
        name: val,
        customerType: 1
      };
      saveBaseShopType(data).then(res => {
        if (res.code == 200) {
          this.getTableList();
          this.dis = false;
        }
      });
    },
    cancel() {
      this.tableData.pop();
      this.dis = false;
    },
    startOrStop(id, status) {
      let data = {
        shopTypeId: id,
        status: status
      };
      updateStatus(data).then(res => {
        if (res.code == 200) {
          this.getTableList();
        }
        this.dis = false;
      });
    },
    tips(msg, type) {
      this.$message({
        type: type,
        message: msg
      });
    }
  }
};
</script>

<style scoped>
.clientTypeMg {
  /*width: 1000px;*/
  padding-top: 28px;
}
</style>
